<template>
  <div>
    <van-swipe style="height: 200px">
      <van-swipe-item v-for="i in 6" :key="i">
        <img
          style="width: 100%; height: 100%"
          src="http://ehall.dgut.edu.cn/static/banner6.a5cff40e.jpg"
          alt=""
          srcset=""
        />
      </van-swipe-item>
    </van-swipe>
    <van-tabs v-model="active" :lazy-render="false">
      <van-tab title="所有问卷" name="1">
        <van-search
          v-model="listQuery.name"
          show-action
          shape="round"
          placeholder="请输入搜索关键词"
        >
          <template #action>
            <div @click="getSurvey">搜索</div>
          </template>
        </van-search>
        <el-card shadow="never" :body-style="{ padding: '20px' }">
          <div slot="header">
            <span style="font-size: 20px">所有问卷</span>
            <span style="font-size: 12px; color: #979797; margin-left: 15px"
              >All Questionnaires</span
            >
          </div>
          <div class="container">
            <div
              @click="showDialog(i)"
              class="item"
              :style="{ 'min-width': b + 'px', 'max-width': b + 'px' }"
              v-for="i in list"
              :key="i.id"
            >
              <div class="box">
                <div class="item-top">
                  <div class="left">
                    <svg style="height: 60px; width: 60px" aria-hidden="true">
                      <use :xlink:href="'#' + JSON.parse(i.setting).icon"></use>
                    </svg>
                  </div>
                  <div class="right">
                    <h4 class="title" :style="{ width: b - 100 + 'px' }">
                      {{ i.title }}
                    </h4>
                    <div style="font-size: 12px; color: #979797">
                      {{ new Date(i.create_time).toLocaleString() }}
                    </div>
                    <el-rate :value="5" disabled></el-rate>
                  </div>
                </div>
                <div class="item-bottom">
                  <span>SURVEY-00{{ i.id }}</span>
                  <span>{{ i.submit_count }}人填写</span>
                </div>
              </div>
            </div>
          </div>
        </el-card>
      </van-tab>
      <van-tab title="已填问卷" name="2">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad()"
        >
          <van-cell
            v-for="item in Submitlist"
            :key="item.id"
            :title="item.survey"
            :value="new Date(item.create_time).toLocaleString()"
            is-link
            @click="
              item.status == '审核不通过'
                ? $router.push({ name: 'WorkUpdate', query: { id: item.id } })
                : null
            "
          >
            <span slot="title">
              <van-tag type="success" v-if="item.status == '审核通过'"
                >审核通过</van-tag
              >
              <van-tag type="danger" v-if="item.status == '审核不通过'"
                >审核不通过</van-tag
              >
              <van-tag type="warning" v-if="item.status == '未审核'"
                >未审核</van-tag
              >
              <span style="word-break: break-all;margin-left: 10px;">{{ item.survey }}</span>
            </span>
          </van-cell>
        </van-list>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { getSurvey, getSubmitSurvey } from "@/api/work";

export default {
  data() {
    return {
      a: false,
      b: "363.5",
      list: [],
      listQuery: {
        name: null,
      },
      listQuery1: {
        page: 1,
        limit: 10,
      },
      active: "1",
      Submitlist: [],
      finished: false,
      loading: false,
    };
  },
  created() {
    this.getSurvey();
    this.onLoad();
  },
  mounted() {
    setTimeout(() => {
      this.withChange();
    }, 100);
    window.onresize = () => {
      this.withChange();
    };
  },
  methods: {
    withChange() {
      let clientWidth =
        document.getElementsByClassName("container")[0].clientWidth;
      if (parseInt(clientWidth / 363.5) > 1) {
        this.b = clientWidth / parseInt(clientWidth / 363.5) - 1;
      } else {
        this.b = clientWidth;
      }
    },
    getSurvey() {
      getSurvey(this.listQuery).then((res) => {
        this.list = res;
      });
    },
    showDialog(i) {
      let setting = JSON.parse(i.setting);
      this.$dialog
        .confirm({
          title: i.title,
          messageAlign: "left",
          message:
            "<div >流程说明：</div>" +
            setting.process +
            "<div>业务说明：</div>" +
            setting.business,
          confirmButtonText: "办理",
          confirmButtonColor: "#49a9ee",
        })
        .then(() => {
          this.$router.push({ name: "Survey", query: { id: i.id } });
          this.dialogshow = false;
        })
        .catch(() => {
          // on cancel
        });
    },
    onLoad() {
      this.loading = true;
      getSubmitSurvey(this.listQuery1).then((res) => {
        this.Submitlist = this.Submitlist.concat(res.results);
        this.loading = false;
        this.listQuery1.page += 1;
        this.finished = !res.next;
      });
    },
  },
};
</script>

<style scoped>
/* .el-card {
  margin: 1.5% 3%;
} */
.container {
  display: flex;
  flex-wrap: wrap;
}
.item-top {
  display: flex;
  border-bottom: 1px solid #ccc;
  margin-bottom: 6px;
}
.item {
  height: 131.4px;
  box-sizing: border-box;
  padding: 5px;
  cursor: pointer;
}
.box {
  transition: all 0.2s ease-out;
  border-radius: 6px;
  padding: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.2);
}
/* .item:hover .box {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.2);
  transform: scale(1.1);
} */
.left img {
  width: 50px;
  height: 50px;
}
.right >>> .el-rate__icon {
  font-size: 12px;
}
.right {
  padding-left: 10px;
}
.right > * {
  margin-bottom: 6px;
}
.item-bottom {
  display: flex;
  justify-content: space-between;
  color: #979797;
  font-size: 12px;
}
.title {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
</style>
